mod common;

use common::{dourl, eq_obj_notime};
use serde_json::json;
use std::{file, line};

#[tokio::test]
async fn a_chunk() {
    let data = json!({
        "n": "chunk,chunk2,chunk3"
    });
    let ret = dourl(&data, "rmtab").await.expect("error_1");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "tab": "chunk2",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "line": "abc",
                }
            }
        ]
    });
    let ret = dourl(&data, "gchunk").await.expect_err("chunk-1");
    assert_eq!(
        ret,
        json!({
            "code": 2003,
            "fail": true,
            "success": false,
            "message": r#"para error: "tag cannot be duplicates: line""#,
        })
    );

    let data = json!({
        "tab": "chunk2",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "msn": "abc",
                    "usn": "p 100 1",
                    "esn": "e001",
                },
                "v": {
                    "msn": "abc",
                }
            }
        ]
    });
    let ret = dourl(&data, "gchunk").await.expect_err("chunk-1");
    assert_eq!(
        ret,
        json!({
            "code": 2100,
            "fail": true,
            "success": false,
            "message": r#"json format error: "field and tag not duplicates: msn""#,
        })
    );

    let data = json!({
        "tab": "chunk2",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "msn": "msn11",
                    "esn": "e001",
                    "usn": "p 100 1",
                }
            },
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "msn": "msn22",
                    "esn": "e001",
                    "usn": "p 100 1",
                }
            },
        ]
    });
    let ret = dourl(&data, "gchunk").await.expect_err("chunk-1");
    assert_eq!(
        ret,
        json!({
            "code": 2001,
            "fail": true,
            "success": false,
            "message": r#"miss error: "v""#,
        })
    );

    let data = json!({
        "tab": "chunk",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "usn": "p 100 1",
                    "esn": "e001",
                },
                "v": {
                    "d1":11,
                    "d2":21,
                    "d3":21,
                }
            },
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "esn": "e001",
                    "usn": "p 100 1",
                },
                "v": {
                    "d1":12,
                    "d2":22,
                    "d3":22,
                }
            },
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "usn": "p 100 1",
                    "esn": "e001",
                },
                "v": {
                    "d1":13,
                    "d2":23,
                    "d3":23,
                }
            },
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "usn": "p 100 1",
                    "esn": "e001",
                },
                "v": {
                    "d1":14,
                    "d2":24,
                    "d3":24,
                }
            },
        ]
    });
    let ret = dourl(&data, "gchunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "tab": "chunk2",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "msn": "msn11",
                    "esn": "e001",
                    "usn": "p 100 1",
                },
                "v": {}
            },
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "msn": "msn22",
                    "esn": "e001",
                    "usn": "p 100 1",
                },
                "v": {}
            },
        ]
    });
    let ret = dourl(&data, "gchunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "n": "chunk,chunk2",
        "obj": "*",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "esn": "e001", "line": "line-one", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0},
                    {"d1": 12, "d2": 22, "d3": 22, "esn": "e001", "line": "line-one", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0},
                    {"d1": 13, "d2": 23, "d3": 23, "esn": "e001", "line": "line-one", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0},
                    {"d1": 14, "d2": 24, "d3": 24, "esn": "e001", "line": "line-one", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0}
                ]},
                "chunk2": {"data": [
                    {"esn": "e001", "line": "line-one", "msn": "msn11", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0},
                    {"esn": "e001", "line": "line-one", "msn": "msn22", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0}
                ]}
            }
        }),
        line!(),
        file!(),
    );

    let data = json!({
        "tab": "chunk3",
        "line": "line-one",
        "data": [
            {
                "psn": "",
                "wsn": "",
                "k": {
                    "task": "abc",
                },
                "v": {}
            }
        ]
    });
    let ret = dourl(&data, "gchunk").await.expect("chunk-1");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "n": "chunk3",
        "obj": "*",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk3": {"data": [
                    {"line": "line-one", "task": "abc", "dtp": 0},
                ]}
            }
        }),
        line!(),
        file!(),
    );
}

#[tokio::test]
async fn b_chunk() {
    let data = json!({
        "n": "chunk,chunk2"
    });
    let ret = dourl(&data, "rmtab").await.expect("error_1");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "tab": "chunk",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "usn": "p 100 1",
                    "esn": "e001",
                },
                "v": {
                    "d1":11,
                    "d2":21,
                    "d3":21,
                }
            },
            {
                "psn": "p1002",
                "wsn": "w001",
                "k": {
                    "esn": "e001",
                    "usn": "p 100 1",
                },
                "v": {
                    "d1":12,
                    "d2":22,
                    "d3":22,
                }
            },
            {
                "psn": "p1003",
                "wsn": "w001",
                "k": {
                    "usn": "p 100 1",
                    "esn": "e001",
                },
                "v": {
                    "d1":13,
                    "d2":23,
                    "d3":23,
                }
            },
            {
                "psn": "p1004",
                "wsn": "w001",
                "k": {
                    "usn": "p 100 1",
                    "esn": "e001",
                },
                "v": {
                    "d1":14,
                    "d2":24,
                    "d3":24,
                }
            },
        ]
    });
    let ret = dourl(&data, "gchunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "tab": "chunk2",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "msn": "msn11",
                    "esn": "e001",
                    "usn": "p 100 1",
                },
                "v": {}
            },
            {
                "psn": "p1002",
                "wsn": "w001",
                "k": {
                    "msn": "msn22",
                    "esn": "e001",
                    "usn": "p 100 1",
                },
                "v": {}
            },
        ]
    });
    let ret = dourl(&data, "gchunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "n": "chunk,chunk2",
        "obj": "*",
        "opt": {"and": [
            {"eq": "psn", "v": "p1001"},
            {"eq": "line", "v": "line-one"},
        ]}
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "esn": "e001", "line": "line-one", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0},
                ]},
                "chunk2": {"data": [
                    {"esn": "e001", "line": "line-one", "msn": "msn11", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0},
                ]}
            }
        }),
        line!(),
        file!(),
    );
}

#[tokio::test]
async fn c_chunk() {
    let data = json!({
        "n": "chunk4"
    });
    let ret = dourl(&data, "rmtab").await.expect("error_1");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "tab": "chunk4",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001 ",
                "wsn": "w001",
                "k": {
                    "usn": "p 100 1",
                    "esn": "e001",
                },
                "v": {
                    "d1":11,
                    "d2":21,
                    "d3":21,
                }
            }
        ]
    });
    let ret = dourl(&data, "gchunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "n": "chunk4",
        "obj": "*",
        "opt": {"and": [
            {"eq": "psn", "v": "p1001 "}
        ]}
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk4": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "esn": "e001", "line": "line-one", "psn": "p1001 ", "usn": "p 100 1", "wsn": "w001", "dtp": 0},
                ]}
            }
        }),
        line!(),
        file!(),
    );
}

#[tokio::test]
async fn d_chunk() {
    let data = json!({
        "n": "chunk5"
    });
    let ret = dourl(&data, "rmtab").await.expect("error_1");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "tab": "chunk5",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001 ",
                "wsn": "w001",
                "k": {
                    "usn": "p 100 1",
                    "msn": " 10004449, VOITH, 21935, 29047054",
                    "esn": "e001",
                },
                "v": {
                    "d1":11,
                    "d2":21,
                    "d3":21,
                }
            }
        ]
    });
    let ret = dourl(&data, "gchunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "n": "chunk5",
        "obj": "*",
        "opt": {"and": [
            {"eq": "psn", "v": "p1001 "}
        ]}
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk5": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "esn": "e001", "line": "line-one", "psn": "p1001 ", "usn": "p 100 1", "wsn": "w001", "dtp": 0, "msn": " 10004449, VOITH, 21935, 29047054"},
                ]}
            }
        }),
        line!(),
        file!(),
    );
}

#[tokio::test]
async fn e_chunk() {
    let data = json!({
        "n": "chunk5"
    });
    let ret = dourl(&data, "rmtab").await.expect("error_1");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "tab": "chunk5",
        "ck": false,
        "data": [
            {
                "k": {
                    "usn": "p 100 1",
                    "msn": " 10004449, VOITH, 21935, 29047054",
                    "esn": "e001",
                },
                "v": {
                    "d1":11,
                    "d2":21,
                    "d3":21,
                }
            }
        ]
    });
    let ret = dourl(&data, "gchunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "n": "chunk5",
        "obj": "*",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk5": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "esn": "e001", "usn": "p 100 1", "dtp": 0, "msn": " 10004449, VOITH, 21935, 29047054"},
                ]}
            }
        }),
        line!(),
        file!(),
    );
}
